#!/bin/sh
# 编译安装NTP
App=ntp
AppName=NTP
AppBase=/App
AppDir=$AppBase/$App
AppProg=$AppDir/bin/ntpd
AppConf=$AppDir/ntp.conf

AppSrcBase=/App/src
AppSrcFile=$App-*.tar.*
AppSrcDir=$(find $AppSrcBase -maxdepth 1 -name "$AppSrcFile" -type f 2> /dev/null | sed -e 's/.tar.*$//' -e 's/^.\///')

AppUser=$App
AppGroup=$AppUser
AppDriftFile=$(grep "^driftfile" $AppConf 2> /dev/null | awk '{print $2}')
AppLogFile=$(grep "^logfile" $AppConf 2> /dev/null | awk '{print $2}')
AppPidFile=$(grep "^pidfile" $AppConf 2> /dev/null | awk '{print $2}')
Options="-u $AppUser:$AppGroup -g"

RemoveFlag=0
InstallFlag=0

# 获取PID
fpid()
{
    AppPid=$(ps ax | grep "$AppProg" | grep -v "grep" | awk '{print $1}' 2> /dev/null)
}

# 查询状态
fstatus()
{
    fpid

    if [ -f "$AppProg" ]; then
        echo "$AppName 已安装"
        if [ -z "$AppPid" ]; then
            echo "$AppName 未启动"
        else
            echo "$AppName 正在运行"
        fi
    else
        echo "$AppName 未安装"
    fi
}

# 删除
fremove()
{
    fpid
    RemoveFlag=1

    if [ -z "$AppPid" ]; then
        if [ -d "$AppDir" ]; then
            rm -rf $AppDir && echo "删除 $AppName"
        else
            echo "$AppName 未安装"
        fi
    else
        echo "$AppName 正在运行" && exit 1
    fi
}

# 备份
fbackup()
{
    Day=$(date +%Y-%m-%d)
    BackupFile=$App-$Day.tgz

    if [ -f "$AppProg" ]; then
        cd $AppBase
        tar zcvf $BackupFile --exclude=*.log --exclude=*.drift $App
        [ $? -eq 0 ] && echo "$AppName 备份成功" || echo "$AppName 备份失败"
    else
        echo "$AppName 未安装"
    fi
}

# 安装
finstall()
{
    fpid
    InstallFlag=1

    if [ -z "$AppPid" ]; then
        test -f "$AppProg" && echo "$AppName 已安装"
        [ $? -ne 0 ] && fupdate && fcpconf
    else
        echo "$AppName 正在运行"
    fi
}

# 拷贝配置
fcpconf()
{
    cp -vf --backup=numbered $ScriptDir/ntp.conf $AppConf
}

# 更新
fupdate()
{
    Operate="更新"
    [ $InstallFlag -eq 1 ] && Operate="安装"
    [ $RemoveFlag -ne 1 ] && fbackup

    cd $AppSrcBase
    test -d "$AppSrcDir" && rm -rf $AppSrcDir

    tar zxf $AppSrcFile || tar jxf $AppSrcFile
    cd $AppSrcDir
    ./configure --prefix=$AppDir --enable-all-clocks --enable-parse-clocks --enable-linuxcaps && make && make install

    if [ $? -eq 0 ]; then
        echo "$AppName $Operate成功"
    else
        echo "$AppName $Operate失败"
        exit 1
    fi

}

# 初始化
finit()
{
    echo "初始化 $AppName"

    id -gn $AppGroup &> /dev/null
    if [ $? -ne 0 ]; then
        groupadd $AppGroup && echo "新建 $AppName 运行组：$AppGroup"
    else
        echo "$AppName 运行组：$AppGroup 已存在"
    fi

    id -un $AppUser &> /dev/null
    if [ $? -ne 0 ]; then
        useradd -s /bin/false -M -g $AppGroup $AppUser
        if [ $? -eq 0 ]; then
            echo "新建 $AppName 运行用户：$AppUser"
            echo "l7bz321#789" | passwd --stdin $AppUser &> /dev/null
        fi
    else
        echo "$AppName 运行用户：$AppUser 已存在"
    fi

    AppDriftDir=$(dirname $AppDriftFile 2> /dev/null)
    if [ -n "$AppDriftDir" ]; then
        if [ -d "$AppDriftDir" ]; then
            echo "$AppName 时间漂移文件目录：$AppDriftDir 已存在"
        else
            mkdir -p $AppDriftDir && echo "新建 $AppName 时间漂移文件目录：$AppDriftDir"
        fi

        chown -R $AppUser:$AppGroup $AppDriftDir
        [ $? -eq 0 ] && echo "修改时间漂移文件目录：$AppDriftDir 拥有者为 $AppUser, 属组为 $AppGroup"
    fi

    AppLogDir=$(dirname $AppLogFile 2> /dev/null)
    if [ -n "$AppLogDir" ]; then
        if [ -d "$AppLogDir" ]; then
            echo "$AppName 日志目录：$AppLogDir 已存在"
        else
            mkdir -p $AppLogDir && echo "新建 $AppName 日志目录：$AppLogDir"
        fi
    fi

    AppPidDir=$(dirname $AppPidFile 2> /dev/null)
    if [ -n "$AppPidDir" ]; then
        if [ -d "$AppPidDir" ]; then
            echo "$AppName PID文件目录：$AppPidDir 已存在"
        else
            mkdir -p $AppPidDir && echo "新建 $AppName PID 文件目录：$AppPidDir"
        fi
    fi
}

# 启动
fstart()
{
    fpid

    if [ -n "$AppPid" ]; then
        echo "$AppName 正在运行"
    else
        $AppProg -c $AppConf $Options &
        [ $? -eq 0 ] && echo "启动 $AppName" || echo "$AppName 启动失败"
    fi
}

# 停止
fstop()
{
    fpid

    if [ -n "$AppPid" ]; then
        echo "$AppPid" | xargs kill -9
        if [ $? -eq 0 ]; then 
            echo "停止 $AppName"
        else
            echo "$AppName 停止失败"
        fi
    else
        echo "$AppName 未启动"
    fi
}

# 重启
frestart()
{
    fpid
    [ -n "$AppPid" ] && fstop
    fstart
}

ScriptDir=$(dirname $0)
ScriptFile=$(basename $0)
case "$1" in
    "install"   ) finstall;;
    "update"    ) fupdate;;
    "reinstall" ) fremove && finstall;;
    "remove"    ) fremove;;
    "backup"    ) fbackup;;
    "init"      ) finit;;
    "start"     ) fstart;;
    "stop"      ) fstop;;
    "restart"   ) frestart;;
    "status"    ) fstatus;;
    "cpconf"    ) fcpconf;;
    *           )
    echo "$ScriptFile install              安装 $AppName"
    echo "$ScriptFile update               更新 $AppName"
    echo "$ScriptFile reinstall            重装 $AppName"
    echo "$ScriptFile remove               删除 $AppName"
    echo "$ScriptFile backup               备份 $AppName"
    echo "$ScriptFile init                 初始化 $AppName"
    echo "$ScriptFile start                启动 $AppName"
    echo "$ScriptFile stop                 停止 $AppName"
    echo "$ScriptFile restart              重启 $AppName"
    echo "$ScriptFile status               查询 $AppName 状态"
    echo "$ScriptFile cpconf               拷贝 $AppName 配置"
    ;;
esac
